﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class UpdateMemo : BasePage
    {
        int contractID;
        View_Contracts contracts;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            contracts = (View_Contracts)Session[Request.QueryString[KEY]];
            string oldFilter = contracts.Filter;

            contractID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
                contracts.Filter = Products.ColumnNames.ID + "=" + contractID.ToString();

            contracts.Filter = oldFilter;
        }

        protected void btnSaveAndClose_OnClick(object sender, EventArgs e)
        {
            try
            {
                string oldFilter = contracts.Filter;

                contractID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
                if (mode != ADD_MODE)
                    contracts.Filter = Products.ColumnNames.ID + "=" + contractID.ToString();

                string memo = updateMemo.Memo + contracts.Memo;
                Contracts.UpdateMemo(contractID, memo);
                contracts.Memo = memo;

                contracts.Filter = oldFilter;

                CloseDlg(1);
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
            }
        }
    }
}
